package cn.kgc.mes.WmIssueHeader.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.kgc.mes.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoRequest;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;
import org.springframework.beans.BeanUtils;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.mes.WmIssueHeader.domain.vo.WmIssueHeaderVo;
import cn.kgc.mes.WmIssueHeader.domain.WmIssueHeader;
import cn.kgc.mes.WmIssueHeader.service.WmIssueHeaderService;

/**
 * 生产领料单头业务
 * 
 * @author jiyupo
 * @date 2025-09-15
 */
@Component
public class WmIssueHeaderBusiness {

    @Resource
    WmIssueHeaderService settingService;


    /**
     * 分页
     *
     * @param vo
     * @return
     */
    public CommonDataPageResponse<WmIssueHeaderVo> page(WmIssueHeaderVo vo) {
        CommonDataPageResponse<WmIssueHeaderVo> response = new CommonDataPageResponse<>();

        //分页
        Long current = 1L;
        Long size = 10L;

        PageDtoRequest reqPageDto = vo.getPageDto();
        if (reqPageDto != null) {
            current = reqPageDto.getCurrent() != null ? reqPageDto.getCurrent() : 1L;
            size = reqPageDto.getSize() != null ? reqPageDto.getSize() : 10L;
        }

        Page page = new Page<>(current, size);

        LambdaQueryWrapper<WmIssueHeader> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(StringUtils.hasText(vo.getIssueCode()), WmIssueHeader::getIssueCode, vo.getIssueCode());

        queryWrapper.like(StringUtils.hasText(vo.getIssueName()), WmIssueHeader::getIssueName, vo.getIssueName());

        queryWrapper.eq(vo.getWorkstationId() != null, WmIssueHeader::getWorkstationId, vo.getWorkstationId());

        queryWrapper.eq(StringUtils.hasText(vo.getWorkstationCode()), WmIssueHeader::getWorkstationCode, vo.getWorkstationCode());

        queryWrapper.like(StringUtils.hasText(vo.getWorkstationName()), WmIssueHeader::getWorkstationName, vo.getWorkstationName());

        queryWrapper.eq(vo.getWorkorderId() != null, WmIssueHeader::getWorkorderId, vo.getWorkorderId());

        queryWrapper.eq(StringUtils.hasText(vo.getWorkorderCode()), WmIssueHeader::getWorkorderCode, vo.getWorkorderCode());

        queryWrapper.eq(vo.getTaskId() != null, WmIssueHeader::getTaskId, vo.getTaskId());

        queryWrapper.eq(StringUtils.hasText(vo.getTaskCode()), WmIssueHeader::getTaskCode, vo.getTaskCode());

        queryWrapper.eq(vo.getClientId() != null, WmIssueHeader::getClientId, vo.getClientId());

        queryWrapper.eq(StringUtils.hasText(vo.getClientCode()), WmIssueHeader::getClientCode, vo.getClientCode());

        queryWrapper.like(StringUtils.hasText(vo.getClientName()), WmIssueHeader::getClientName, vo.getClientName());

        queryWrapper.eq(StringUtils.hasText(vo.getClientNick()), WmIssueHeader::getClientNick, vo.getClientNick());

        queryWrapper.eq(vo.getWarehouseId() != null, WmIssueHeader::getWarehouseId, vo.getWarehouseId());

        queryWrapper.eq(StringUtils.hasText(vo.getWarehouseCode()), WmIssueHeader::getWarehouseCode, vo.getWarehouseCode());

        queryWrapper.like(StringUtils.hasText(vo.getWarehouseName()), WmIssueHeader::getWarehouseName, vo.getWarehouseName());

        queryWrapper.eq(vo.getLocationId() != null, WmIssueHeader::getLocationId, vo.getLocationId());

        queryWrapper.eq(StringUtils.hasText(vo.getLocationCode()), WmIssueHeader::getLocationCode, vo.getLocationCode());

        queryWrapper.like(StringUtils.hasText(vo.getLocationName()), WmIssueHeader::getLocationName, vo.getLocationName());

        queryWrapper.eq(vo.getAreaId() != null, WmIssueHeader::getAreaId, vo.getAreaId());

        queryWrapper.eq(StringUtils.hasText(vo.getAreaCode()), WmIssueHeader::getAreaCode, vo.getAreaCode());

        queryWrapper.like(StringUtils.hasText(vo.getAreaName()), WmIssueHeader::getAreaName, vo.getAreaName());

        queryWrapper.eq(vo.getIssueDate() != null, WmIssueHeader::getIssueDate, vo.getIssueDate());

        queryWrapper.eq(StringUtils.hasText(vo.getStatus()), WmIssueHeader::getStatus, vo.getStatus());

        queryWrapper.eq(StringUtils.hasText(vo.getAttr1()), WmIssueHeader::getAttr1, vo.getAttr1());

        queryWrapper.eq(StringUtils.hasText(vo.getAttr2()), WmIssueHeader::getAttr2, vo.getAttr2());

        queryWrapper.eq(vo.getAttr3() != null, WmIssueHeader::getAttr3, vo.getAttr3());

        queryWrapper.eq(vo.getAttr4() != null, WmIssueHeader::getAttr4, vo.getAttr4());

        queryWrapper.orderByDesc(WmIssueHeader::getIssueId);

        IPage<WmIssueHeader> pageRecords = settingService.page(page, queryWrapper);

        //分页记录
        List<WmIssueHeader> records = pageRecords.getRecords();
        //转为vo
        List<WmIssueHeaderVo> voList = new ArrayList<>();
        for (WmIssueHeader record : records) {
            WmIssueHeaderVo settingVo  = new WmIssueHeaderVo();

            BeanUtils.copyProperties(record, settingVo);

            voList.add(settingVo);
        }
        response.setRecords(voList);

        //分页信息
        PageDtoResponse pageDto = new PageDtoResponse();
        pageDto.setCurrent(current);
        pageDto.setSize(size);
        pageDto.setTotal(pageRecords.getTotal());
        pageDto.setPages(pageRecords.getPages());

        response.setPageDto(pageDto);

        return response;
    }

    /**
     * 信息
     *
     * @param vo
     * @return
     */
    public WmIssueHeaderVo info(WmIssueHeaderVo vo) {

        WmIssueHeaderVo settingVo = new WmIssueHeaderVo();

        Long issueId = vo.getIssueId();

        WmIssueHeader record = settingService.getById(issueId);

        BeanUtils.copyProperties(record, settingVo);

        return settingVo;
    }

    /**
     * 添加
     *
     * @param vo
     * @return
     */
    public StatusResponse add(WmIssueHeaderVo vo) {
        StatusResponse response = new StatusResponse();

        WmIssueHeader record = new WmIssueHeader();

        BeanUtils.copyProperties(vo, record);

        settingService.save(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 更新
     *
     * @param vo
     * @return
     */
    public StatusResponse update(WmIssueHeaderVo vo) {
        StatusResponse response = new StatusResponse();

        WmIssueHeader record = new WmIssueHeader();

        BeanUtils.copyProperties(vo, record);

        settingService.updateById(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     */
    public StatusResponse delete(WmIssueHeaderVo vo) {
        StatusResponse response = new StatusResponse();

        Long issueId = vo.getIssueId();

        settingService.removeById(issueId);

        response.setSuccess(true);

        return response;
    }

}
